import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

/**
 * The base class used go gather data from professors.
 * @author 
 *
 */
public class ProfessorReferenceGenerator {

	String startingPage = "http://www.info.iastate.edu/individuals/advanced";
	String basePath = "http://www.info.iastate.edu";
	
	ArrayList<String> departmentList = new ArrayList<String>();
	ArrayList<String> professorLinks = new ArrayList<String>();
	
	/**
	 * Gathers professors and stores them in the data base
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void gatherProfessors() throws MalformedURLException, IOException{
		
		//Creates a page class to gather departments from
		ProfessorListPage departmentPage = new ProfessorListPage(startingPage);
		//stores the list of departments in departmentList
		departmentList = departmentPage.gatherDepartmentNames();
		
		for(String s : departmentList){
			this.gatherProfessorsForDepartment(s);
		}
		
		
		//list to hold professorModel
		ArrayList<ProfessorModel> professorData = new ArrayList<ProfessorModel>();
		
		//Foreach of the departments found create a page parse professorData
		for(String s : professorLinks){
			//loads and parses page into xml
			ProfessorDataPage rpd = new ProfessorDataPage(basePath + s);
			//gathers data from xml
			ProfessorModel pm = rpd.gatherInitialInformation();
			//adds data to class
			professorData.add(pm);
		}
		
	
		
		System.out.println();
		System.out.println("In total, " + professorLinks.size() + " professors.");
		
	}
	
	/*
	 * Private helper method that uses ProfessorList Page
	 * 
	 * 1) Gathers first page of professors for department
	 * 
	 * 2)Checks to see if there are more pages for department( if not returns)
	 * 
	 * 3)If so, finds which pages need to be searched and which pages have been searched already
	 * 
	 * 4)Finds more professors on additional pages - Go to step 2
	 * 
	 */
	private void gatherProfessorsForDepartment(String dept){
		//Pages not yet searched
		ArrayList<String> toSearch = new ArrayList<String>();
		//Pages already searched
		ArrayList<String> searched = new ArrayList<String>();
		//List of professors found
		ArrayList<String> departmentProfessors = new ArrayList<String>();
		
		//makes complete url using helper method
		String deptUrl = urlForDepartment(dept);
		System.out.print("Gathering For ");
		System.out.print(dept + " :");
		
		//adds first page to the searched list
		toSearch.add(deptUrl);
		while(toSearch.size() > 0){
			try {
				//removes the first search listing
				String url = toSearch.get(0);
				
				//some string formatting issues
				String new_url = url.replaceAll("&amp;", "&");
				
				//uses professorListPage class to search page
				ProfessorListPage rg = new ProfessorListPage(basePath + new_url);
				
				//removes page from toSearch and adds it to searches if page is loaded
				toSearch.remove(0);
				searched.add(url);
				
				//Gathers professor list from loaded pages
				ArrayList<String> newProfessorLinks = rg.gatherProfessorLinks();
				
				//checks to see if there are more pages to search
				boolean hasMorePages = rg.hasMorePages();
				
				if(hasMorePages){
					//finds other pages
					ArrayList<String> otherLinks = rg.gatherOtherLinks();
					for(String s:otherLinks){
						//checks to see if other pages either have been searched or need to be searched
						if(toSearch.contains(s) == false && searched.contains(s) == false){
							//if neither, add to toSearch
							toSearch.add(s);
						}
					}
				}
				//copies new links to professor links
				for(String s:newProfessorLinks){
					departmentProfessors.add(s);
				}
				
			} 
			catch (MalformedURLException e) {
	
				e.printStackTrace();
			} 
			catch (IOException e) {
				
				System.out.print("%");
			}catch (NoProfessorException e) {
				
				System.out.println(e.message);
			}
			System.out.print("*");
		}
		System.out.println();
		System.out.println("Department Finished: " + departmentProfessors.size() + " new professors added!");
		System.out.println();
		
		for(String s : departmentProfessors){
			//if professorlinks are already not in the list of professor links, adds them
			if(!professorLinks.contains(s))
			{
				professorLinks.add(s);
			}
		}
		
		
	}
	
	
	/*
	 * Private helper method for taking a departmentString and turning it into a full url
	 */
	private static String urlForDepartment(String departmentString){
		String firstHalf = "/individuals/advanced?last_name=&first_name=&email=&department=";
		String secondHalf = "&individual_type=faculty_staff";
		String replaceSpaces = departmentString.replaceAll(" ", "%20");
		return firstHalf + replaceSpaces + secondHalf;
		//return "http://www.info.iastate.edu/individuals/advanced?last_name=&first_name=&email=&department=Computer%20Engineering&individual_type=faculty_staff&orderby=0&orderdir=0&page=2";
		
	}
	
}
